import java.util.PriorityQueue;

public class test {


  //返回数组中前k个最小的值
  public int[]  smallK(int []array,int k){
      PriorityQueue<Integer> queue=new PriorityQueue<>(array.length);
      for (int i = 0; i < array.length; i++) {
          queue.offer(array[i]);
      }
      int[] ret=new int[k];
      for (int i = 0; i < k; i++) {
          ret[i]=queue.poll();
      }
      return ret;
  }
    //另外一种时间复杂度比较低的写法(例：
    // 返回前k个最大值

    public int[]  MaxK(int []array,int k){
        int[] ret=new int[k];
      if (array==null||k<=0) {
          return ret;
      }
        PriorityQueue<Integer> queue=new PriorityQueue<>(array.length);
        for (int i = 0; i < k; i++) {
            queue.offer(array[i]);
        }
        for (int i = k; i <array.length ; i++) {
            int top=queue.peek();
            if (array[i]>top){
                queue.poll();
                queue.offer(array[i]);
            }
        }
        for (int i = 0; i <k ; i++) {
            ret[i]=queue.poll();
        }
        return ret;
    }
    public static void main(String[] args) {
        TestHeap heap=new TestHeap();
        int[] array={12,34,23,56,45,78};
        heap.initHeap(array);
        heap.createHeap();
    }
}
